package com.yjhh.chdataservice.service;

import com.yjhh.chdataservice.component.HDFSManager;
import com.yjhh.chdataservice.except.YJHHException;
import com.yjhh.chdataservice.repository.FileUploadRecordRepository;
import com.yjhh.chdataservice.util.CommonResultData;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 件管理ServiceImpl
 */
@Service
public class FileManagerServiceImpl implements FileManagerService {

    @Autowired
    private FileUploadRecordRepository fileUploadRecordRepository;

    @Autowired
    private HDFSManager hdfsManager;

    @Override
    public CommonResultData<String> handleFileUpload(String remoteIP, MultipartFile[] files, String[] fileSrcParentPaths) throws YJHHException, IOException {
        if (files == null || files.length <= 0
                || fileSrcParentPaths == null || fileSrcParentPaths.length <= 0 || files.length != fileSrcParentPaths.length) {
            return new CommonResultData<>(HttpStatus.SC_BAD_REQUEST, "params error");
        }
        for (int i = 0; i < files.length; i++) {
            MultipartFile file = files[i];
            String srcParentPath = fileSrcParentPaths[i];
            hdfsManager.uplaodFile(srcParentPath, file.getName(), file.getInputStream());
        }
        return new CommonResultData<>(HttpStatus.SC_OK, "suc");
    }
}
